/*
 * 
 * Copyright 2011 visual-analysis-of-cancer.
 * This file is part of visual-analysis-of-cancer.
 * 
 * visual-analysis-of-cancer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * visual-analysis-of-cancer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * visual-analysis-of-cancer:
 * An tool to analye the WHO and GLOBOCAN cancer data.
 * http://code.google.com/p/visual-analysis-of-cancer/
 * 
 * 
 * 
 * Authors: Erik Sniegula, Johannes Dahlke
 * 
 * Module:  Programming in Processing for Visual Analytics
 * Group:   20
 * 
 * Created: Aug. 2011
 * Version: 1.0
 *
 */
 




package de.fuberlin.processing.who.cancer.gui.scatterplot;

import java.util.Collection;

import controlP5.CDrawable;
import controlP5.ControlEvent;
import controlP5.ControlListener;
import controlP5.ControlP5;
import controlP5.ControlP5Constants;
import controlP5.Controller;
import controlP5.ControllerGroup;
import controlP5.ControllerInterface;

import processing.core.PApplet;

public abstract class CustomCell extends Controller implements Cell {

	private static final int COLOR_FRAME = ControlP5.papplet.color( 0, 54, 82);
	private static final int COLOR_MOUSEOVER = ControlP5.papplet.color( 170, 224, 252);
	protected static final int COLOR_SELECTED = ControlP5.papplet.color( 245, 64, 119);
	protected static final int COLOR_NORMAL = ControlP5.papplet.color( 0, 0xAA);
	protected static final float STROKEWEIGHT_SELECTED = 1;
	protected static final float STROKEWEIGHT_NORMAL = 0.01f;
	protected static final int STROKE_ALPHA_SELECTED = 200;
	protected static final int STROKE_ALPHA_NORMAL = 60;
	
	
	private boolean selected = false;
	private boolean highlighted = false;
	protected float margin;
	protected float left;
	protected float top;
	private ControllerGroup parent;
	private CellConstructionAndProcessingParcel constructionAndProcessingParcel;
	
	Collection coll;
	
	public CustomCell( final ControlP5 theControlP5, final ControllerGroup theParent, final String theName, CellConstructionAndProcessingParcel cellConstructionAndProcessingParcel, Collection coll, final float theX, final float theY, final int theWidth, final int theHeight) {
		super( theControlP5, theParent, theName, theX, theY, theWidth, theHeight);
		this.coll = coll;
		this.left = position.x;
		this.top = position.y;
		this.margin = (width+height)*0.5f*0.05f;
		this.parent = theParent;
		this.setBroadcast( true);
		this.constructionAndProcessingParcel = cellConstructionAndProcessingParcel;
	}


	@Override
	public void beforeDraw(PApplet p) {
		p.pushStyle();
		if ( highlighted) { 
	    // grey rect
		  p.fill( 0xDC);
		  p.noStroke();
		  p.rect( this.position.x +1,  this.position.y +1, width -2, height -2);
		}
		p.popStyle();
		
		p.pushStyle();
		p.stroke(COLOR_NORMAL);
		p.smooth();
		p.strokeWeight(4);
		p.strokeJoin( p.ROUND);
    p.strokeCap( p.ROUND);
	}
	
	@Override
	public void afterDraw(PApplet p) {
   	p.noFill();	
		if ( isMouseOver()) {
		  p.stroke( COLOR_MOUSEOVER);
		  p.strokeWeight( 3f);
		} else if ( selected) {
			p.stroke( COLOR_SELECTED);
		  p.strokeWeight( 2f);
		} else {  
		  p.stroke( COLOR_FRAME);
		  p.strokeWeight( 0.25f);
		}
		p.rect( this.position.x +1,  this.position.y +1, width -2, height -2);
		p.popStyle();
	}

	@Override
	public Controller setValue( float theValue) {
		_myValue = theValue;
		return null;
	}
	
	@Override
	public abstract void drawCell( PApplet p);
	
	
	
	@Override
	public final void draw( PApplet p) {
		beforeDraw( p);
		drawCell( p);
		afterDraw( p);
	}
	
	
	public void select() {
		selected = true;
		update();
	}
	
	public void unselect() {
		selected = false;
		update();		
	}

	public void highlight() {
		highlighted = true;
		update();
	}
	
	public void beNormal() {
		highlighted = false;
		update();		
	}
	
	@Override
	protected void mousePressed() {
		setValue( EVENT_MOUSE_PRESSED);
		broadcast( ControlP5Constants.FLOAT);
	  super.mousePressed();
	}
	
	
	@Override
	public CellConstructionAndProcessingParcel getCellConstructionAndProcessingParcel() {
		return constructionAndProcessingParcel;
	}
	
	
	
	

}
